/*
 * ContactMenu.java
 *
 * Created on 17 Р�СЋРЅСЊ 2011 Рі., 0:17
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.jimm.protocol;

import org.jimm.app.ChatActivity;
import org.jimm.app.ContactListActivity;
import org.jimm.app.JimmApplication;
import org.jimm.app.R;
import org.jimm.message.PlainMessage;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * @author Vladimir Kryukov
 */
public class ContactMenu {
    private Contact contact;
    private Protocol protocol;
    ContactListActivity activity;


    public ContactMenu(Protocol p, Contact c, ContactListActivity a) {
        activity = a;
        contact = c;
        protocol = p;
    }

    public ContextMenu getContextMenu(ContextMenu menu) {
        contact.initContextMenu(menu);

        return menu;
    }

    @SuppressWarnings("unused")
    public void doAction(int cmd) {
        switch (cmd) {
            case Contact.USER_MENU_MESSAGE: /* Send plain message */
                activity.openChat(contact);
                break;

            case Contact.USER_MENU_QUOTE: /* Send plain message with quotation */
            case Contact.USER_MENU_PASTE: /* Send plain message without quotation */
                //protocol.getChat(contact).writeMessage(JimmUI.getClipBoardText(Contact.USER_MENU_QUOTE == cmd));
                Intent intent = new Intent(activity, ChatActivity.class);
                intent.putExtra(ChatActivity.CONTACT_ID_EXTRA, contact.getUserId());
                intent.putExtra(ChatActivity.ACCOUNT_ID_EXTRA, contact.getProtocol().getUserId());
                intent.putExtra(ChatActivity.MSG_TEXT_EXTRA, JimmApplication.getClipBoardText(Contact.USER_MENU_QUOTE == cmd));
                activity.startActivity(intent);
                break;

            case Contact.USER_MENU_COPY_UID:
                JimmApplication.setClipBoardText(contact.getName(), contact.getUserId());
                break;

            case Contact.USER_MENU_ADD_USER:
                //protocol.getSearchForm().show(contact.getUserId());
                break;

            case Contact.USER_MENU_USER_REMOVE: {
                // #sijapp cond.if modules_HISTORY is "true" #
                //HistoryStorage.getHistory(contact).removeHistory();
                // #sijapp cond.end#
                String text = JimmApplication.getContext().getString(R.string.remove_contact);

                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setMessage(text + "?")
                        .setCancelable(false)
                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                protocol.removeContact(contact);
                            }
                        })
                        .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = builder.create();
            }
            //ContactList.getInstance().activate();
            break;

            case Contact.USER_MENU_STATUSES: /* Show user statuses */
                protocol.showStatus(contact);
                break;

            case Contact.USER_MENU_WAKE:
                protocol.sendMessage(contact, PlainMessage.CMD_WAKEUP, true);
                //protocol.getChat(contact).activate();
                break;

            // #sijapp cond.if modules_FILES is "true"#
            //case Contact.USER_MENU_FILE_TRANS:
            //   // Send a filetransfer with a file given by path
            //   new FileTransfer(protocol, contact).startFileTransfer();
            //   break;
            //    
            //case Contact.USER_MENU_CAM_TRANS:
            //    // Send a filetransfer with a camera image
            //    new FileTransfer(protocol, contact).startPhotoTransfer();
            //   break;
            // #sijapp cond.end#

            case Contact.USER_MENU_RENAME:
                /* Rename the contact local and on the server
                   Reset and display textbox for entering name */
            {
                final Dialog renameDialog = new Dialog(activity);
                final EditText editNick = (EditText) renameDialog.findViewById(R.id.edit_rename);

                renameDialog.setContentView(R.layout.login);
                renameDialog.setTitle(R.string.rename);
                editNick.setText(contact.getName());

                Button buttonOk = (Button) renameDialog.findViewById(R.id.button_ok);
                buttonOk.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        String nick = editNick.getText().toString();

                        protocol.renameContact(contact, nick);
                        renameDialog.dismiss();
                    }

                });
                renameDialog.show();
                break;
            }

            // #sijapp cond.if modules_HISTORY is "true" #
            case Contact.USER_MENU_HISTORY: /* Stored history */
                showHistory();
                break;
            // #sijapp cond.end #

            case Contact.USER_MENU_MOVE: {
                final int count = protocol.getGroupItems().size();
                final String[] groups = new String[count];

                for (int i = 0, j = 0; i < count; i++) {
                    Group g = protocol.getGroupItems().elementAt(i);
                    if (contact.equals(g.getName())) {
                        groups[j] = g.getName();
                        j++;
                    }
                }

                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setTitle(R.string.select_group);
                builder.setSingleChoiceItems(groups, -1, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        Group g = protocol.getGroup(groups[item]);
                        protocol.moveContactTo(contact, g);
                    }
                });
                AlertDialog alert = builder.create();
                break;
            }
            case Contact.USER_MENU_USER_INFO:
                protocol.showUserInfo(contact, activity);
                break;


            case Contact.USER_MENU_REQU_AUTH: /* Request auth */
                protocol.requestAuth(contact.getUserId());
                //Jimm.getJimm().getDisplay().closeMenus();
                break;

            case Contact.USER_MENU_GRANT_AUTH:
                protocol.grandAuth(contact.getUserId());
//                protocol.getChat(contact).resetAuthRequests();
                //Jimm.getJimm().getDisplay().closeMenus();
                break;

            case Contact.USER_MENU_DENY_AUTH:
                protocol.denyAuth(contact.getUserId());
//                protocol.getChat(contact).resetAuthRequests();
                // Jimm.getJimm().getDisplay().closeMenus();
                break;

            default:
                protocol.doAction(contact, cmd);
        }
    }

    // #sijapp cond.if modules_HISTORY is "true" #
    private final void showHistory() {
//        if (contact.hasHistory()) {
//            HistoryStorage history;
//            if (contact.hasChat()) {
//                history = protocol.getChat(contact).getHistory();
//            } else {
//                history = HistoryStorage.getHistory(contact);
//            }
//            new HistoryStorageList(history, contact).show();
//        }
    }
    // #sijapp cond.end#
}
